﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web.Mvc;
using MethodWorx.CMS.Plugins;
using MethodWorx.CMS.UI.Application.ActionResults;
using MethodWorx.CMS.Content;
using MethodWorx.CMS.Services;

namespace MethodWorx.CMS.Workflow.Validate
{
    public class ValidateConfigurationController : Controller
    {
        private IdeExtension view;
        private WorkflowService service;

        public ValidateConfigurationController(IdeExtension view, WorkflowService service)
        {
            this.view = view;
            this.service = service;
        }

        [HttpGet]
        public ActionResult Invoke(Page instance, string workflow, Guid? stepId, Guid? parentStepId)
        {
            this.ViewData["ComparisonOptions"] = this.GetComparisonOptions();
            return new EmbeddedViewResult(view.WebEditor, this.ViewData, Step(instance, workflow, stepId, parentStepId).ComponentConfiguration);
        }

        private SelectList GetComparisonOptions()
        {
            var items = new string[] {"Equals", "Not Equals", "Greater Than", "Less Than"};
            return new SelectList(items);
        }

        [HttpPost]
        public ActionResult Invoke(Page instance, string workflow, string submit, Guid? stepId, Guid? parentStepId)
        {
            this.ViewData["ComparisonOptions"] = this.GetComparisonOptions();
            var model = Step(instance, workflow, stepId, parentStepId);

            this.TryUpdateModel<ValidateConfiguration>((ValidateConfiguration)model.ComponentConfiguration);

            if ((submit ?? "").ToLower() == "cancel")
                return new CloseDialogResult();

            if (ModelState.IsValid)
            {
                //
                //  ok, everything is ok, we need to save the workflow
                this.service.SaveWorkflow(instance, workflow, stepId, parentStepId, model);
                return new CloseDialogResult("saved");
            }

            return new EmbeddedViewResult(view.WebEditor, this.ViewData, model.ComponentConfiguration);
        }

        private WorkflowConfigurationStep Step(Page instance, string workflowName, Guid? stepId, Guid? parentStepId)
        {
            var workflow = instance.GetWorkflowConfiguration(true).FirstOrDefault(w => w.Name.ToLower() == workflowName.ToLower());
            if (workflow != null)
            {
                if (stepId == null)
                {
                    Guid id = Guid.NewGuid();
                    return new WorkflowConfigurationStep()
                        {
                            Id = id,
                            ComponentId = ValidateWorkflowComponent.ComponentId,
                            ComponentConfiguration = new ValidateConfiguration()
                        };
                }
                else
                {
                    return workflow.FindStep(stepId.Value);
                }
            }
            
            return null;
        }
    }
}
